package pchi.pchicamera.helpers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.util.LruCache;
import android.widget.ImageView;

public class BitmapHelpers {
	/*
	 * Resizing image size
	 */
	public static Bitmap decodeFile(String filePath, float WIDTH, float HEIGHT) {
		try {

			File file = new File(filePath);

			BitmapFactory.Options o = new BitmapFactory.Options();
			o.inJustDecodeBounds = true;

			BitmapFactory.decodeStream(new FileInputStream(file), null, o);

			final float REQUEST_WIDTH = WIDTH;
			final float REQUEST_HEIGHT = HEIGHT;

			int scale = 1;

			while (o.outWidth / scale / 2 >= REQUEST_WIDTH
					&& o.outHeight / scale / 2 >= REQUEST_HEIGHT) {
				scale *= 2;
			}

			BitmapFactory.Options o2 = new BitmapFactory.Options();
			o2.inSampleSize = scale;

			return BitmapFactory.decodeStream(new FileInputStream(file), null,
					o2);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	private static final List<String> IMAGE_EXTS = Arrays.asList("jpg", "jpeg",
			"png", "gif", "bmp");

	public static boolean isImage(String path) {
		int start = path.lastIndexOf(".");
		if (start == -1)
			return false;

		String ext = path.substring(start + 1, path.length());

		if (IMAGE_EXTS.contains(ext.toLowerCase(Locale.getDefault())))
			return true;

		return false;
	}

	static final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

	// Use 1/8th of the available memory for this memory cache.
	static final int cacheSize = maxMemory / 8;

	private static LruCache<String, Bitmap> memoryCache = new LruCache<String, Bitmap>(
			cacheSize) {
		@SuppressLint("NewApi")
		@Override
		protected int sizeOf(String key, Bitmap bitmap) {
			// The cache size will be measured in kilobytes rather than
			// number of items.
			return bitmap.getByteCount() / 1024;
		}
	};

	public static void addBitmapToMemoryCache(String key, Bitmap bitmap) {
		if (getBitmapFromMemCache(key) == null) {
			memoryCache.put(key, bitmap);
		}
	}

	public static Bitmap getBitmapFromMemCache(String key) {
		return memoryCache.get(key);
	}

	public static void loadBitmap(String path, ImageView view, float width,
			float height) {
		final Bitmap bitmap = getBitmapFromMemCache(buildKey(path, width,
				height));
		if (bitmap != null) {
			if (view != null)
				view.setImageBitmap(bitmap);
		} else {
			DisplayImageAsync task = new DisplayImageAsync(path, view, width,
					height);

			// mImageView.setImageResource(R.drawable.image_placeholder);
			// BitmapWorkerTask task = new BitmapWorkerTask(mImageView);
			task.execute();
		}

	}

	public static String buildKey(String path, float width, float height) {
		// return path + "_" + width + "_" + height;
		return path;
	}
}
